|
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Tutorial 2: Programming Beyond Visual Basic
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Understanding the Windows API |
|
| Source | |
The Windows Application Programming Interface (API) is a set of hundreds of predefined functions that are built into the dynamic link libraries (DLLs) and which make up the Windows operating system. End users cannot access these functions; however, programmers can access the code written in the DLLs through the API and use this code in their programs.
A DLL (dynamic link library) is compiled code that can be accessed only by other programs at the programming level. You put functions and subroutines into DLLs so that you may share them among many programs running at the same time. DLLs are created in languages such as C(++) and Object Pascal (Delphi). The most often used DLLs in the API for 32-bit Windows operating systems are Kernel32.DLL, User32.DLL, and GDI32.DLL. You also can make DLLs in VB, but they are called ActiveX DLLs. For more information about making ActiveX DLLs in VB, see Chapter 26:"Making Object-Oriented Programs with Visual Basic" from the source text Practical Visual Basic 6.
You can program any type of Visual Basic project in order to access the Windows API if you use the Declare statement in the General Declarations section of a module. For example, you may access GetCursorPos, which locates the onscreen mouse pointer position. It is defined for use within Visual Basic as follows:
Declare Function GetCursorPos Lib "user32" _ Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
In this syntax:
|
Declare |
is a keyword indicating an external procedure declaration. |
|
Function |
is the procedure type. |
|
GetCursorPos |
is the name of this particular external procedure. |
|
Lib |
is a keyword indicating the DLL in which to locate the function. |
|
"user32" |
is the The DLL (must be enclosed in quotation marks, no file extension required, and must be in the path-that is, the program must know where to find the DLL). |
|
Alias |
is a keyword that indicates a friendly name by which you can refer to the function. |
|
"GetCursorPos" |
is the friendly name. |
|
LpPoint |
is an argument variable. |
|
As POINTAPI/As Long |
is a keyword indicating argument type. |
|
POINTAPI |
is an argument type. |
|
Long |
is a function return type |
As you can see, GetCursorPos resides in the DLL User32.DLL. The function takes one argument, lpPoint, which is a Windows-defined type: POINTAPI. The function returns a Long.
You do not need any tools other than VB itself in order to use the Windows API. Working with the API requires a great deal of extra knowledge, however. If you plan on doing any coding with the Windows API, you will need a copy of The Microsoft Developers Network CD-ROM, which ships with the Professional and Enterprise versions of Visual Basic and documents all the available Windows API functions. The CD-Rom is an extensive work that provides a large amount of sample code. Sadly, most of the code is in C, so you might need a good third-party Visual Basic book that deals with the Windows API.
|
|
||
|
||||||
|
|||
|
|||
|
| |
| |
| |
|
| |
| |
|
© Copyright 2000 InformIT. All rights reserved. |